﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SpaceWars
{
    public class Stage
    {
        public Background bg;
        public PlayerShip playerShip;
        public Sprite[] bulletList;
        public Enemy[] enemyList;
        public double[] spawnTime;
        public double currentTime;
        public int currentObj;
        public bool active;
        protected int bulletNum;
        public Vector2 SCREEN_SIZE;
        public int killCount;
        public double endTime;

        public Stage(Vector2 screen, Texture2D bgTex, Texture2D playerTex, Texture2D bulletTex, int maxBullets, Enemy[] list, double[] times)
        {
            SCREEN_SIZE = screen;
            bulletList = new Sprite[maxBullets];
            enemyList = new Enemy[list.Length];
            spawnTime = new double[times.Length];
            bg = new Background(screen, bgTex);
            playerShip = new PlayerShip(screen, playerTex, SCREEN_SIZE / 2.0f, Vector2.Zero, new Vector2(32.0f ),
                                        new Vector2(16.0f), 0.0, false );
            for (int i = 0; i < maxBullets; i++)
                bulletList[i] = new Sprite(screen, bulletTex);
            for (int i = 0; i < list.Length; i++){
                enemyList[i] = list[i];
                spawnTime[i] = times[i];
            }
            killCount = 0;
            currentTime = 0.0;
            currentObj = 0;
            endTime = 15.0;

            active = false;
        }
        public void update( double elapsedTime, SoundBank sound ){
            if (active){
                bulletNum = playerShip.maxBullets - playerShip.bulletsLeft;
                currentTime += elapsedTime;
                bg.update(elapsedTime);
                playerShip.update(elapsedTime);
                // Activate the next enemy based on time
                if (spawnTime[currentObj] <= currentTime && currentObj < enemyList.Length-1 ){
                    enemyList[currentObj].alive = true;
                    currentObj++;
                }
                if( currentObj > enemyList.Length )
                    endTime -= elapsedTime;
                if (endTime <= 0.0)
                    stop();
                for (int i = 0; i < bulletNum; i++){
                    for( int j=0; j<currentObj; j++ )
                        if (bulletList[i].collides(enemyList[j])){
                            bulletList[i].kill();
                            enemyList[j].kill();
                            killCount++;
                            //sound.PlayCue("explode");
                        }
                }
                for( int i=0; i<currentObj; i++ )
                    if (enemyList[i].collides(playerShip)){
                        playerShip.health--;
                        enemyList[i].kill();
                    }
                if (playerShip.health <= 0)
                    stop();
                for (int i = 0; i < currentObj; i++)
                    if (enemyList[i].alive)
                        enemyList[i].update(elapsedTime);
                for (int i = 0; i < bulletNum; i++)
                    if (bulletList[i].alive)
                        bulletList[i].update(elapsedTime);
            }
        }

        public void fire(){
            if (playerShip.bulletsLeft == 0)
                return;
            bulletNum = playerShip.maxBullets - playerShip.bulletsLeft;
            bulletList[bulletNum].alive = true;
            bulletList[bulletNum].size = new Vector2( 4.0f );
            bulletList[bulletNum].origin = new Vector2( 2.0f);
            Vector2 offset = new Vector2((float)Math.Cos(playerShip.angle), (float)Math.Sin(playerShip.angle));
            bulletList[bulletNum].loc = playerShip.loc + offset * playerShip.origin.X;
            bulletList[bulletNum].vel = new Vector2((float)Math.Cos(playerShip.angle), (float)Math.Sin(playerShip.angle));
            bulletList[bulletNum].vel *= 300.0f;
            playerShip.bulletsLeft--;
        }

        public void draw(SpriteBatch sb){
            bg.draw(sb);
            if( playerShip.alive)
                playerShip.draw(sb);
            for (int i = 0; i < currentObj; i++)
                if (enemyList[i].alive)
                    enemyList[i].draw(sb);
            for (int i = 0; i < bulletNum; i++)
                if (bulletList[i].alive)
                    bulletList[i].draw(sb);
        }

        public void start(){ 
            active = true;
            playerShip.alive = true;
        }
        public void stop(){
            active = false;
            playerShip.alive = false;
            for (int i = 0; i < bulletList.Length; i++)
                bulletList[i].kill();
            for (int i = 0; i < enemyList.Length; i++)
                enemyList[i].kill();
        }
    }
}
